// 
//  
//  Copyright (C) 2004-2009  Autodesk, Inc.
//  
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of version 2.1 of the GNU Lesser
//  General Public License as published by the Free Software Foundation.
//  
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
#include "stdafx.h"
#include "MapInfoDescribeSchema.h"
#include "MapInfoConnection.h"
#include "FdoCommonSchemaUtil.h"

#include <FdoCommonMiscUtil.h>

//-------------------------------------------------------
// Constructor / destructor
//-------------------------------------------------------


// constructs a DescribeSchema command using the specified connection
MapInfoDescribeSchema::MapInfoDescribeSchema(MapInfoConnection* connection)
: MapInfoCommand<FdoIDescribeSchema>(connection)
{
    m_classNames = NULL;
}


// default destructor
MapInfoDescribeSchema::~MapInfoDescribeSchema()
{
}

//-------------------------------------------------------
// FdoIDescribeSchema implementation
//-------------------------------------------------------


// Gets the name of the schema to describe.  This is optional,
// if not specified execution of the command will describe all schemas.
FdoString* MapInfoDescribeSchema::GetSchemaName()
{
    return m_schemaName;
}


// Sets the name of the schema to describe.  This is optional, if not
// specified execution of the command will describe all schemas.
void MapInfoDescribeSchema::SetSchemaName(FdoString* value)
{
    m_schemaName = value;
}

FdoStringCollection* MapInfoDescribeSchema::GetClassNames()
{
    return m_classNames;
}

void MapInfoDescribeSchema::SetClassNames(FdoStringCollection* value)
{
}

FdoFeatureSchemaCollection* MapInfoDescribeSchema::Execute()
{
    // verify connection
    if (m_connection == NULL)
        throw FdoCommandException::Create(NlsMsgGetMain(FDO_NLSID(MAPINFOPROVIDER_NO_CONNECTION)));

    FdoPtr<FdoFeatureSchemaCollection> ret;
    FdoPtr<FdoFeatureSchemaCollection> schemaCollection = m_connection->DescribeSchema();
    if (schemaCollection.p != NULL)
    {
        // handle missing schema better than the FdoCommonSchemaUtil message of FDO_4_UNREADY
        if (0 != m_schemaName.GetLength ())
        {
            FdoPtr<FdoFeatureSchema> schema = schemaCollection->FindItem (m_schemaName);
            if (schema == NULL)
                throw FdoException::Create(NlsMsgGetMain(MAPINFOPROVIDER_SCHEMA_NOT_EXIST, "Schema '%1$ls' not found.", (FdoString*)m_schemaName));
        }

        // Make a copy of the requested schema (or all schemas if no schemaname is provided), and return it:
        ret = FdoCommonSchemaUtil::DeepCopyFdoFeatureSchemas(schemaCollection, m_schemaName);
        VALIDATE_POINTER(ret);
    }
    else
        if (0 != m_schemaName.GetLength ())
            throw FdoException::Create(NlsMsgGetMain(MAPINFOPROVIDER_SCHEMA_NOT_EXIST, "Schema '%1$ls' not found.", (FdoString*)m_schemaName));
        else
            ret = FdoFeatureSchemaCollection::Create (NULL);

    return FDO_SAFE_ADDREF(ret.p);
}
